package com.lengyu.vxminiprogram.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lengyu.vxminiprogram.common.AuthUtil;
import com.lengyu.vxminiprogram.common.BasePageParamDto;
import com.lengyu.vxminiprogram.common.ConstData;
import com.lengyu.vxminiprogram.common.HttpResultDto;
import com.lengyu.vxminiprogram.convert.ICarConverter;
import com.lengyu.vxminiprogram.dto.CarDto;
import com.lengyu.vxminiprogram.dto.TakePeopleDto;
import com.lengyu.vxminiprogram.dto.UserDto;
import com.lengyu.vxminiprogram.entity.Car;
import com.lengyu.vxminiprogram.entity.TakePeople;
import com.lengyu.vxminiprogram.entity.User;
import com.lengyu.vxminiprogram.security.rest.dto.JwtUser;
import com.lengyu.vxminiprogram.service.ICarService;
import com.lengyu.vxminiprogram.service.ITakePeopleService;
import com.lengyu.vxminiprogram.service.IUserService;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liuyu
 * @since 2024-11-19
 */
@RestController
@RequestMapping("/car")
public class CarController {
    @Autowired
    ICarService carService;

    @Autowired
    ICarConverter carConverter;

    @Autowired
    IUserService userService;

    @Autowired
    ITakePeopleService takePeopleService;

    @PostMapping(value="/saveCar")
    public HttpResultDto<String> saveCar(@RequestBody Car car){
        JwtUser user = AuthUtil.getCurrentJwtUser();
        if(StringUtils.isBlank(car.getId())){
            car.setUserId(user.getId());
            car.setVxOpenId(user.getId());
        }
        carService.saveOrUpdate(car);
        return HttpResultDto.success(car.getId());
    }


        //参数1(/user/page)指明了访问该接口需要的url，参数2®指明了访问该接口需要的权限。
    @PostMapping(value="/page")
    public HttpResultDto<Page<CarDto>> page(@RequestBody BasePageParamDto pageParamDto){
        QueryWrapper queryWrapper = new QueryWrapper();
        if(pageParamDto.getParamMapObj() != null){

            for(String key:pageParamDto.getParamMapObj().keySet()){
                if(StringUtils.isNotBlank(String.valueOf(pageParamDto.getParamMapObj().get(key)))){
                    queryWrapper.like(key,pageParamDto.getParamMapObj().get(key));
                }
            }
        }
        queryWrapper.orderByDesc("create_time");
        Page<Car> entityPage = carService.page(new Page<>(pageParamDto.getPageIndex(), pageParamDto.getPageSize()),queryWrapper);
        Page<CarDto> dtoPage = carConverter.pageEntityToDto(entityPage);
        if(dtoPage.getRecords() != null && dtoPage.getRecords().size() > 0){
            dtoPage.getRecords().forEach(i->{
                User user = userService.getById(i.getUserId());
                if(user != null){
                    i.setUserName(user.getVxNickName());
                }
            });
        }
        return HttpResultDto.success(dtoPage);
    }

    /**
     * 查询某个人上的车
     * @param pageParamDto
     * @return
     */
    @PostMapping(value="/takePageCar")
    public HttpResultDto<Page<CarDto>> takePageCar(@RequestBody BasePageParamDto pageParamDto){
        QueryWrapper queryWrapper = new QueryWrapper();
        if(pageParamDto.getParamMapObj() != null){
            for(String key:pageParamDto.getParamMapObj().keySet()){
                if(StringUtils.isNotBlank(String.valueOf(pageParamDto.getParamMapObj().get(key)))){
                    queryWrapper.like(key,pageParamDto.getParamMapObj().get(key));
                }
            }
        }
        queryWrapper.orderByDesc("create_time");
        queryWrapper.select("DISTINCT car_id,create_time");
        Page<TakePeople> entityPage = takePeopleService.page(new Page<>(pageParamDto.getPageIndex(), pageParamDto.getPageSize()),queryWrapper);

        if(entityPage.getRecords() != null && entityPage.getRecords().size() > 0){
            List<String> carIdList = new ArrayList<>();
            entityPage.getRecords().forEach(i->{
                carIdList.add(i.getCarId());
            });
            queryWrapper = new QueryWrapper();
            queryWrapper.in("id",carIdList);
            queryWrapper.orderByDesc("create_time");
            Page<Car> carPage = carService.page(new Page<>(pageParamDto.getPageIndex(), pageParamDto.getPageSize()),queryWrapper);
            Page<CarDto> dtoPage = carConverter.pageEntityToDto(carPage);
            if(dtoPage.getRecords() != null && dtoPage.getRecords().size() > 0){
                dtoPage.getRecords().forEach(i->{
                    User user = userService.getById(i.getUserId());
                    if(user != null){
                        i.setUserName(user.getVxNickName());
                    }
                });
            }
            return HttpResultDto.success(dtoPage);
        }
        else{
            return HttpResultDto.success(new Page<>(pageParamDto.getPageIndex(), pageParamDto.getPageSize()));
        }

    }
}
